package com.wzy.business.store.main

import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import android.widget.RelativeLayout
import com.just.agentweb.AgentWeb
import com.wangzhenyu.common.base.BaseActivity
import com.wangzhenyu.common.base.BasePreImpl
import com.wangzhenyu.common.base.BasePresenter
import com.wangzhenyu.common.base.IConstant
import com.wangzhenyu.common.view.ITextView
import com.wzy.business.R

class UserServiceH5Activity : BaseActivity<BasePresenter>() {

    private var mType: Int? = null
    private lateinit var ivService: ImageView

    private lateinit var tvAgreeService: ITextView


    companion object {

        const val KEY_VIEW_TYPE = "KEY_VIEW_TYPE"
        private const val VIEW_PRIVACY = 1
        private const val VIEW_SERVICE = 2
        private const val VIEW_SECURITY = 3

        fun service(context: Activity, result: Int) {
            val intent = Intent(context, UserServiceH5Activity::class.java)
            intent.putExtra(KEY_VIEW_TYPE, VIEW_SERVICE)
            context.startActivityForResult(intent, result)
        }

        fun privacy(context: Activity, result: Int) {
            val intent = Intent(context, UserServiceH5Activity::class.java)
            intent.putExtra(KEY_VIEW_TYPE, VIEW_PRIVACY)
            context.startActivityForResult(intent, result)
        }

        fun security(context: Activity, result: Int) {
            val intent = Intent(context, UserServiceH5Activity::class.java)
            intent.putExtra(KEY_VIEW_TYPE, VIEW_SECURITY)
            context.startActivityForResult(intent, result)
        }
    }

    override fun initBundles() {
        mType = intent?.getIntExtra(KEY_VIEW_TYPE, VIEW_PRIVACY)
    }

    override fun createPresenter(): BasePresenter {
        return BasePreImpl(this)
    }

    override fun initView() {
        mToolBarDelegate?.setMidTitle("C店商家用户协议")

        ivService = findViewById(R.id.ivService)
        tvAgreeService = findViewById(R.id.tvAgreeService)

        ivService.isSelected = true
        ivService.setOnClickListener { ivService.isSelected = !ivService.isSelected }
        tvAgreeService.setOnClickListener {
            if (!ivService.isSelected) {
                showToast("请同意服务条款")
                return@setOnClickListener
            }
            setResult(Activity.RESULT_OK)
            finish()
        }
        var url = IConstant.getUserServiceH5()
        when (mType) {
            VIEW_PRIVACY -> {
                url = IConstant.getPrivacyServiceH5()
                mToolBarDelegate?.setMidTitle("C店隐私政策")
            }

            VIEW_SECURITY -> {
                mToolBarDelegate?.setMidTitle("C店服务保障金协议和说明")
                url = IConstant.getSecurityH5()
            }
        }
        AgentWeb.with(this)
            .setAgentWebParent(
                findViewById<RelativeLayout>(R.id.rlServiceRoot),
                RelativeLayout.LayoutParams(-1, -1)
            )
            .useDefaultIndicator()
            .createAgentWeb()
            .ready()
            .go(url)

    }

    override fun getLayoutId(): Int {
        return R.layout.activity_user_service_h5
    }
}